package cn.jolyoulu.common.web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.jolyoulu.common.web.encrypt.AesEncryptFilter;
import cn.jolyoulu.common.web.filter.MultiReadHttpServletFilter;

/**
 * @Author LuZhouJin
 * @Date 2023/5/25
 * 公共过滤器配置
 */
@Slf4j
@Configuration
public class FilterCommConfig {

    /**
     * 可重复读，可重复写的 HttpServlet
     * @return
     */
    @Bean
    public FilterRegistrationBean<MultiReadHttpServletFilter> multiReadHttpServletFilter(){
        FilterRegistrationBean<MultiReadHttpServletFilter> bean = new FilterRegistrationBean<>();
        bean.setOrder(1);
        bean.setFilter(new MultiReadHttpServletFilter());
        bean.addUrlPatterns("/*");
        return bean;
    }

    /**
     * 加密解密过滤器
     * @return
     */
//    @Bean
    public FilterRegistrationBean<AesEncryptFilter> aesEncryptFilter(){
        //加密密钥
        String key = "8e1291fd6f3d4dde95a7a24819dce1d1";
        log.info("AesEncryptFilter key ==> {}",key);
        FilterRegistrationBean<AesEncryptFilter> bean = new FilterRegistrationBean<>();
        bean.setOrder(1);
        bean.setFilter(new AesEncryptFilter(key,true,"",6000L));
        bean.addUrlPatterns("/*");
        return bean;
    }
}
